<?php

namespace App\Http\Controllers\Api\Spa\Admin;

use Illuminate\Http\Request;
// use Illuminate\Http\Response;
// use Illuminate\Support\Facades\Crypt;
// use Illuminate\Support\Facades\Hash;
// use Illuminate\Support\Facades\Auth;
// use Illuminate\Foundation\Auth\ThrottlesLogins;
// use Illuminate\Validation\ValidationException;
// use Illuminate\Support\Facades\Log;
use App\Http\Models\Spa\Admin\Article as ArticleModel;
// use Illuminate\Support\Facades\DB;
use App\Utils\JsonResponse;

/**
 * Class LoginController
 * 
 * @package App\Http\Controllers\Api\Spa\Admin
 */
class ArticleOneController extends \App\Http\Controllers\Controller
{
	public function getOne(Request $request) {
		// $article = ArticleModel::
		// $articles = DB::table('articles')->get();
		// $article = DB::table('articles')->where('id', 'e80af0df-4044-448e-8eb2-64857e8c942a')->first();
		// 都成功；
		// dd($article);
		// var_dump($article->id);
		// var_dump($article->so_title);
		
		$article = ArticleModel::where('id', 'e80af0df-4044-448e-8eb2-64857e8c942a')->first();
		dd($article);
		// var_dump($article->id);// int(0)
		// var_dump($article->so_status);// int(0)
		// var_dump($article->so_title);// string(8) "title001"
		// var_dump($article->created_at);// object(Illuminate\Support\Carbon)#325 (3) { ["date"]=> string(26) "2020-08-31 17:35:47.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
	}

	public function getList(Request $request) {
		$page = $request->query('page', 1);
		$pageSize = $request->query('pageSize', $this->pageSize ?? intval(env('ARTICLE_PAGE_SIZE') ?: env('PAGE_SIZE')));

		$articles = ArticleModel::paginate($pageSize);
		// dd($articles);

		/*
		表里没有数据时
		Illuminate\Pagination\LengthAwarePaginator {#635 ▼
		  #total: 0
		  #lastPage: 1
		  #items: Illuminate\Database\Eloquent\Collection {#1106 ▼
		    #items: []
		  }
		  #perPage: 10
		  #currentPage: 1
		  #path: "http://spa.laravel7120.cm/api/article/list"
		  #query: []
		  #fragment: null
		  #pageName: "page"
		  +onEachSide: 3
		  #options: array:2 [▼
		    "path" => "http://spa.laravel7120.cm/api/article/list"
		    "pageName" => "page"
		  ]
		}
		var_dump($articles->total);// Cannot access protected property Illuminate\Pagination\LengthAwarePaginator::$total
		var_dump($articles->getTotal());// Method Illuminate\Database\Eloquent\Collection::getTotal does not exist.
		var_dump($articles->total());// 0
		*/
		

		/*
		有一条数据
		Illuminate\Pagination\LengthAwarePaginator {#1253 ▼
		  #total: 1
		  #lastPage: 1
		  #items: Illuminate\Database\Eloquent\Collection {#1043 ▼
		    #items: array:1 [▼
		      0 => App\Http\Models\Spa\Admin\Article {#1045 ▼
		         	#connection: "mysql"
						#table: "articles"
						#primaryKey: "id"
						#keyType: "int"
						+incrementing: true
						#with: []
						#withCount: []
						#perPage: 15
						+exists: true
						+wasRecentlyCreated: false
						#attributes: array:11 [
							"id" => "e80af0df-4044-448e-8eb2-64857e8c942a"
							"so_title" => "1"
							"so_description" => ""
							"so_content" => null
							"so_status" => 0
							"create_admin_id" => 0
							"created_at" => "2020-08-31 17:35:47"
							"update_admin_id" => 0
							"updated_at" => null
							"delete_admin_id" => 0
							"deleted_at" => null
						]
						#original: array:11 [
							"id" => "e80af0df-4044-448e-8eb2-64857e8c942a"
							"so_title" => "1"
							"so_description" => ""
							"so_content" => null
							"so_status" => 0
							"create_admin_id" => 0
							"created_at" => "2020-08-31 17:35:47"
							"update_admin_id" => 0
							"updated_at" => null
							"delete_admin_id" => 0
							"deleted_at" => null
						]
						#changes: []
						#casts: []
						#classCastCache: []
						#dates: []
						#dateFormat: null
						#appends: []
						#dispatchesEvents: []
						#observables: []
						#relations: []
						#touches: []
						+timestamps: true
						#hidden: []
						#visible: []
						#fillable: []
						#guarded: array:1 [▼
						  0 => "*"
						]
		      }
		    ]
		  }
		  #perPage: 10
		  #currentPage: 1
		  #path: "http://spa.laravel7120.cm/api/article/list"
		  #query: []
		  #fragment: null
		  #pageName: "page"
		  +onEachSide: 3
		  #options: array:2 [▼
		    "path" => "http://spa.laravel7120.cm/api/article/list"
		    "pageName" => "page"
		  ]
		}
		var_dump($articles->total());// 1
		dd($articles->items());
		dd($articles->items()[0]);// App\Http\Models\Spa\Admin\Article { ...
		dd($articles->items()[0]->attributes);// null;
		dd($articles->items()[0]->attributes());// Call to undefined method App\Http\Models\Spa\Admin\Article::attributes()
		dd($articles->items()[0]->attribute('id'));// Call to undefined method App\Http\Models\Spa\Admin\Article::attribute()
		dd($articles->items()[0]->id);// 
		*/
		// $m = $articles->items()[0];
		// dd($m);
		// var_dump($m->getOriginal('id'));
		// var_dump($m->id);
		return JsonResponse::response([
			'total' => $articles->total(),
			'items' => $articles->items()
		]);
	}
}